<?php
class SettingsDABase extends AbstractDbObject
{
	/**
	 * Table name
	 *
	 * @var string
	 */
	protected $tableName = 'settings';

	/**
	 * Table alias
	 *
	 * @var string
	 */
	protected $tableAlias = 'settings';

	/**
	 * Create a state
	 *
	 * @access public
	 *
	 * @param SettingsEntity $stateEntity: object
	 *
	 * @return primary key has just been inserted
	 */
	public function create($settingsEntity)
	{
		try {
			// Get db object
			$dbWriter = $this->getDbWriter();

			// Fetch array
			$data = array(
				'name' => $settingsEntity->name,
				'value' => $settingsEntity->value,
				'ownerId' => $settingsEntity->ownerId,				
			);

			// Insert into database
			$dbWriter->insert($this->tableName, $data);

			return true;			
		} catch(Exception $ex) {
			throw $ex;
		}
	}

	/**
	 * Update a settings
	 *
	 * @access public
	 *
	 * @param SettingsEntity $settingsEntity: object
	 *
	 * @return bool true if update success otherwise return false
	 */
	public function update($settingsEntity)
	{
		try {
			// Get db object
			$dbWriter = $this->getDbWriter();

			// Fetch array
			$data = array(				
				'value' => $settingsEntity->value,
			);

			// Update settings
			
			$where[] = "name = '$settingsEntity->name'";
			$where[] = "ownerId = '$settingsEntity->ownerId'";

			// Execute query
			$numRows = $dbWriter->update($this->tableName, $data, $where);
			if($numRows > 0){
				return true;
			}else{
				return false;
			}
		} catch(Exception $ex) {
			throw $ex;
		}
	}

	/**
	 * Delete a settings by Id
	 *
	 * @access public
	 *
	 * @param int $id: Id of settings
	 *
	 * @return bool whether delete action is success or not
	 */
	public function del($id)
	{
		try {
			// Get db object
			$dbWriter = $this->getDbWriter();

			// Execute query
			$rowCount = $dbWriter->delete($this->tableName, "name = '$id'");

			// Return boolean result
			if($rowCount > 0){
				return true;
			}else{
				return false;
			}
		} catch(Exception $ex) {
			throw $ex;
		}
	}

	/**
	 * Get all rows of settings table with search criterias
	 *
	 * @access public
	 *
	 * @param array $arrElement: array of search criterias
	 * @param string $orderBy: Field and the way ordered (ASC or DESC)
	 * @param int $begin: Begin item
	 * @param int $limit: Number rows to query
	 *
	 * @return array of SettingsEntity object
	 */
	public function getItems($arrElement, $orderBy = null, $begin = null, $limit = null)
	{
		// Get db object
		$dbWriter = $this->getDbWriter();

		// Query for get all items of settings with search criterias
		$query = "
			SELECT {$this->tableName}.*
			FROM {$this->tableName}";

		// Add where
		$query .= $this->buildWhereClause($arrElement, $dbWriter, $this->tableName);
		
		// Add order By
		$query .= $this->buildOrderClause($orderBy);

		// Add begin and Limit
		$query .= $this->buildLimitClause($begin, $limit);

		// Execute query
		$result = $dbWriter->fetchAll($query);

		$buffer = null;
		foreach ($result as $element) {
			$settingsEntity = new SettingsEntity($element);

			// Add SettingsEntity into result
			$buffer[] = $settingsEntity;
		}
		return $buffer;
	}

	/**
	 * Get total rows of settings
	 *
	 * @access public
	 *
	 * @param array $arrElement: array of search criterias
	 *
	 * @return int total: total rows of settings
	 */
	public function getTotals($arrElement)
	{
		// Get db object
		$dbWriter = $this->getDbWriter();

		// Query for get total records in settings table
		$query = "
			SELECT COUNT(*) as total
			FROM {$this->tableName} ";
		// Add where
		$query .= $this->buildWhereClause($arrElement, $dbWriter);

		// Execute query
		$result = $dbWriter->fetchRow($query);

		if( !is_null($result) ) {
			return (int) $result['total'];
		}

		// Return no record
		return 0;
	}
}